package com.jlwl.houseservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.Order;
import com.jlwl.houseservice.service.OrderService;
import com.jlwl.houseservice.vo.OrderDetailVO;
import com.jlwl.houseservice.vo.OrderQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员订单控制器
 */
@RestController
@RequestMapping("/admin/orders")
@PreAuthorize("hasRole('ADMIN')")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取订单列表
     */
    @GetMapping
    public Result<Page<Order>> list(OrderQueryVO queryVO) {
        Page<Order> page = orderService.adminPageList(queryVO);
        return Result.success(page);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{orderNo}")
    public Result<OrderDetailVO> getDetail(@PathVariable String orderNo) {
        OrderDetailVO detailVO = orderService.adminGetOrderDetail(orderNo);
        if (detailVO == null) {
            return Result.error("订单不存在");
        }
        return Result.success(detailVO);
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/{orderNo}/status")
    public Result<Void> updateStatus(@PathVariable String orderNo, @RequestParam String status) {
        boolean success = orderService.adminUpdateOrderStatus(orderNo, status);
        if (!success) {
            return Result.error("更新订单状态失败");
        }
        return Result.success();
    }

    /**
     * 分配员工
     */
    @PutMapping("/{orderNo}/assign")
    public Result<Void> assignEmployee(@PathVariable String orderNo, @RequestParam Integer employeeId) {
        boolean success = orderService.adminAssignEmployee(orderNo, employeeId);
        if (!success) {
            return Result.error("分配员工失败");
        }
        return Result.success();
    }
} 